﻿using System;
using System.Collections;

namespace ElectronicCommerce.Framework.Ioc
{
    /// <summary>
    /// Service Provider
    /// </summary>
    public class ServiceProvider
    {
        private static IServiceProviderEx _serviceProvider;

        /// <summary>
        /// Get Service
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public static object GetService(Type serviceType)
        {
            if (_serviceProvider != null)
            {
                object instance = _serviceProvider.GetService(serviceType);
                if (instance != null)
                {
                    return instance;
                }
            }

            try
            {
                return Activator.CreateInstance(serviceType);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(string.Format("Could not create instance of type '{0}'", serviceType.Name), ex);
            }
        }

        /// <summary>
        /// GetService
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static T GetService<T>()
        {
            T item = default(T);

            try
            {
                item = _serviceProvider.GetService<T>();
                return item;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// GetService
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arguements"></param>
        /// <returns></returns>
        public static T GetService<T>(IDictionary arguements)
        {
            T item = default(T);

            try
            {
                item = _serviceProvider.GetService<T>(arguements);
            }
            catch
            {
            }

            if (item == null)
            {
            }

            return item;
        }

        /// <summary>
        /// GetService
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public static T GetService<T>(Type serviceType)
        {
            return (T)GetService(serviceType);
        }

        /// <summary>
        /// Release
        /// </summary>
        /// <param name="instance"></param>
        public static void Release(object instance)
        {
            if (_serviceProvider != null)
                _serviceProvider.Release(instance);
        }

        /// <summary>
        /// Initializes the locator based on the specified service broker.
        /// </summary>
        /// <param name="serviceProvider">The service broker which will locate services.</param>
        public static void Initialize(IServiceProviderEx serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        /// <summary>
        /// Provider
        /// </summary>
        public static IServiceProviderEx Provider
        {
            get { return _serviceProvider; }
        }
    }
}
